package ecoiot.com.testproject.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by developer on 17-10-31.
 */
@SuppressLint("NewApi")

public class TestView1 extends View {

    public TestView1(Context context) {
        super(context);
    }

    public TestView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //  着色器对象   LinearGradient  指的是两个端点之间的渐变色
        //  Shader.TileMode.CLAMP   Shader.TileMode.MIRROR  Shader.TileMode.REPEAT
         Shader shader = new LinearGradient(50,50,300,300,Color.parseColor("#E91E63"),
                Color.parseColor("#2196F3"), Shader.TileMode.CLAMP);
        //  RadialGradient  从中心向周围辐射渐变色
//        shader = new RadialGradient(175,175,150,Color.parseColor("#E91E63"), Color.parseColor("#2196F3"), Shader.TileMode.CLAMP);
        //  SweepGradient   扫描渐变
//        shader = new SweepGradient(175, 175, Color.parseColor("#E91E63"), Color.parseColor("#2196F3"));
        //  BitmapShader    使用bitmap填充图形    第二个参数是横向的tileMode   第三个是纵向
//        shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        //  开启抗锯齿
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //  设置格式为填充
        paint.setStyle(Paint.Style.FILL);
        //  设置着色器（setColor方法则不再起作用）
        paint.setShader(shader);
        //  画一个椭圆   left,top,right,bottom:左上右下坐标
        canvas.drawOval(50, 50, 300, 300, paint);

        Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1.setStyle(Paint.Style.FILL);
        paint1.setColor(Color.RED);
        //  画一个扇形   left,top,right,bottom:所在椭圆的左上右下坐标
        //  startAngle 是弧形的起始角度（x 轴的正向，即正右的方向，是 0 度的位置；顺时针为正角度，逆时针为负角度），
        //  sweepAngle 是弧形划过的角度
        //  useCenter 是否连接到圆心,true是扇形,否则是弧形
        canvas.drawArc(50,350,300,600,-110,90,true,paint1);

        paint1.setColor(Color.GREEN);
        canvas.drawArc(50,350,300,600,-210,50,true,paint1);

        paint1.setColor(Color.BLUE);
        canvas.drawArc(50,350,300,600,-270,60,true,paint1);

        paint1.setColor(Color.YELLOW);
        canvas.drawArc(50,350,300,600,-20,70,true,paint1);

        paint1.setColor(Color.parseColor("#942BAA"));
        canvas.drawArc(50,350,300,600,-160,20,true,paint1);

        paint1.setColor(Color.parseColor("#30EFEF"));
        canvas.drawArc(50,350,300,600,-140,30,true,paint1);

        paint1.setColor(Color.parseColor("#815318"));
        canvas.drawArc(50,350,300,600,-310,40,true,paint1);

        //  设置离屏缓冲
//        int saveLayer = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
//        canvas.restoreToCount(saveLayer);

        //  设置线宽
        paint.setStrokeWidth(40);
        //  设置线头形状  BUTT 平头、ROUND 圆头、SQUARE 方头
        paint.setStrokeCap(Paint.Cap.BUTT);
        canvas.drawLine(400,50,500,50,paint);

        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawLine(400,150,500,150,paint);

        paint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawLine(400,250,500,250,paint);

        Path path1 = new Path();
        path1.moveTo(550,50);
        path1.lineTo(650,50);
        path1.lineTo(600,150);

        Path path2 = new Path();
        path2.moveTo(550,200);
        path2.lineTo(650,200);
        path2.lineTo(600,300);

        Path path3 = new Path();
        path3.moveTo(550,350);
        path3.lineTo(650,350);
        path3.lineTo(600,450);

        Path path4 = new Path();
        path4.moveTo(50,700);
        path4.lineTo(100,800);
        path4.lineTo(150,750);
        path4.lineTo(200,770);
        path4.lineTo(250,820);

        paint.setStyle(Paint.Style.STROKE);

        //  设置拐角的形状    MITER 尖角、 BEVEL 平角、 ROUND 圆角
        paint.setStrokeJoin(Paint.Join.MITER);
        canvas.drawPath(path1,paint);

        paint.setStrokeJoin(Paint.Join.BEVEL);
        canvas.drawPath(path2,paint);

        paint.setStrokeJoin(Paint.Join.ROUND);
        canvas.drawPath(path3,paint);

        //  设置图像抖动
        paint.setDither(true);
        //  设置图像双线性过滤
        paint.setFilterBitmap(true);

        Path path5 = new Path();
        Path.Direction direction = Path.Direction.CCW;
        path5.addCircle(0,0,5,direction);
        paint.setStrokeWidth(1);

        //  设置轮廓效果  DashPathEffect  将线条变为虚线
        //  intervals 是一个数组,它指定了虚线的格式：数组中元素必须为偶数（最少是 2 个）,按照「画线长度、空白长度、画线长度、空白长度」的顺序排列
        //  phase 是虚线的偏移量
        PathEffect pathEffect = new DashPathEffect(new float[]{10, 5}, 10);
        //  CornerPathEffect    将线条的拐角变为圆角  radius 是圆角的半径
//        PathEffect pathEffect = new CornerPathEffect(20);
        //  DiscretePathEffect  把线条进行随机的偏离,让轮廓变得乱七八糟
        //  segmentLength是用来拼接的每个线段的长度  deviation是偏离量
//        PathEffect pathEffect = new DiscretePathEffect(20, 5);

        //  PathDashPathEffect  将线条变为虚线并使用一个Path来作为虚线的样式
        //  advance是两个相邻的shape段之间的间隔    phase是虚线的偏移     style是用来指定拐弯改变的时候shape的转换方式
        //  TRANSLATE：位移    ROTATE：旋转   MORPH：变体
//        PathEffect pathEffect = new PathDashPathEffect(path5, 40, 0, PathDashPathEffect.Style.TRANSLATE);

        paint.setPathEffect(pathEffect);
        canvas.drawPath(path4,paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
}
